Import commonly used modules and objects
In [1]:
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
Let's look at some functionality of Series
In [2]:
s = Series([4, 7, -5, 3])
s1 = Series([4, 7, -5, 3], index=['d', 'b', 'a', 'c'])
s2 = Series([2, -9, 6, 7], index=['b', 'a', 'c', 'd'])
data = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
s3 = Series(data)
In [3]:
s.values
Out[3]:
In [4]:
s.index
Out[4]:
In [7]:
s1['a']
Out[7]:
In [6]:
s[0] = 9
In [8]:
s1[['a', 'b', 'c']]
Out[8]:
In [9]:
s > 0
Out[9]:
In [10]:
s[s > 0]
Out[10]:
In [11]:
s * 2
Out[11]:
In [12]:
np.exp(s)
Out[12]:
In [13]:
'a' in s1
Out[13]:
In [14]:
s = s.reindex([0, 1, 2, 3, 4])
In [15]:
s
Out[15]:
In [16]:
s.isnull()
Out[16]:
In [17]:
s1 + s2
Out[17]:
In [18]:
s1 * s2
Out[18]:
In [19]:
s1 / s2
Out[19]:
Now lets look into some DataFrame functionality
In [20]:
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002],
'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}
index=['one', 'two', 'three', 'four', 'five']
df = DataFrame(data, index=index)
In [21]:
df
Out[21]:
In [22]:
df.columns
Out[22]:
In [23]:
df['pop']
Out[23]:
In [24]:
df[['state', 'pop']]
Out[24]:
In [25]:
df.ix['one']
Out[25]:
In [26]:
df['debt'] = np.arange(5.)
df
Out[26]:
In [27]:
df.drop('five')
Out[27]:
In [28]:
df.drop('debt', axis=1)
Out[28]:
In [29]:
df['pop'] > 2
Out[29]:
In [30]:
df[df['pop'] > 2]
Out[30]: